package com.warehouse.module.business.controller.admin.classify;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.framework.common.util.object.BeanUtils;
import static com.warehouse.framework.common.pojo.CommonResult.success;

import com.warehouse.framework.excel.core.util.ExcelUtils;

import com.warehouse.framework.apilog.core.annotation.ApiAccessLog;
import static com.warehouse.framework.apilog.core.enums.OperateTypeEnum.*;

import com.warehouse.module.business.controller.admin.classify.vo.*;
import com.warehouse.module.business.dal.dataobject.classify.ClassifyDO;
import com.warehouse.module.business.service.classify.ClassifyService;

@Tag(name = "管理后台 - 分类管理")
@RestController
@RequestMapping("/business/classify")
@Validated
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    @PostMapping("/create")
    @Operation(summary = "创建分类管理")
    @PreAuthorize("@ss.hasPermission('business:classify:create')")
    public CommonResult<Long> createClassify(@Valid @RequestBody ClassifySaveReqVO createReqVO) {
        return success(classifyService.createClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分类管理")
    @PreAuthorize("@ss.hasPermission('business:classify:update')")
    public CommonResult<Boolean> updateClassify(@Valid @RequestBody ClassifySaveReqVO updateReqVO) {
        classifyService.updateClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分类管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:classify:delete')")
    public CommonResult<Boolean> deleteClassify(@RequestParam("id") Long id) {
        classifyService.deleteClassify(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分类管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:classify:query')")
    public CommonResult<ClassifyRespVO> getClassify(@RequestParam("id") Long id) {
        ClassifyDO classify = classifyService.getClassify(id);
        return success(BeanUtils.toBean(classify, ClassifyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分类管理分页")
    @PreAuthorize("@ss.hasPermission('business:classify:query')")
    public CommonResult<PageResult<ClassifyRespVO>> getClassifyPage(@Valid ClassifyPageReqVO pageReqVO) {
        PageResult<ClassifyDO> pageResult = classifyService.getClassifyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassifyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分类管理 Excel")
    @PreAuthorize("@ss.hasPermission('business:classify:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassifyExcel(@Valid ClassifyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassifyDO> list = classifyService.getClassifyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分类管理.xls", "数据", ClassifyRespVO.class,
                        BeanUtils.toBean(list, ClassifyRespVO.class));
    }

}